package org.wlldTest.translate;

import org.dromara.easyai.matrixTools.Matrix;
import org.dromara.easyai.matrixTools.MatrixList;
import org.dromara.easyai.matrixTools.MatrixOperation;

/**
 * @author lidapeng
 * @time 2025/3/7 13:15
 */
public class Test2 {
    private static final MatrixOperation matrixOperation = new MatrixOperation();

    public static void main(String[] args) throws Exception {
        test();
    }

    public static void test() throws Exception {
        Matrix matrix = new Matrix(2, 3, "[8,10,30]#[21,20,-30]#");
        Matrix e = new Matrix(2, 3, "[1.25,0,-1.25]#[-1.25,0,1.25]#");
        float avg = e.getAVG();
        float sd = matrixOperation.getSdByMatrix(e, avg, 0.0000001f);
        System.out.println(avg + ":" + sd);
        for (int i = 0; i < 500; i++) {
            Matrix result = normByRow(matrix);
            System.out.println(result.getString());
            Matrix errorMatrix = matrixOperation.sub(e, result);
            Matrix subMatrix = backNormByRow(errorMatrix, 0.1f);
            matrix = matrixOperation.add(matrix, subMatrix);
        }
        System.out.println("结束================");
        System.out.println(matrix.getString());
    }

    private static Matrix backNormByRow(Matrix matrix, float studyRate) throws Exception {
        int x = matrix.getX();
        MatrixList matrixList = null;
        for (int i = 0; i < x; i++) {
            Matrix row = matrix.getRow(i);
            Matrix errorMatrix = back(row, studyRate);
            if (i == 0) {
                matrixList = new MatrixList(errorMatrix, true);
            } else {
                matrixList.add(errorMatrix);
            }
        }
        return matrixList.getMatrix();
    }

    private static Matrix normByRow(Matrix matrix) throws Exception {
        int x = matrix.getX();
        MatrixList matrixList = null;
        for (int i = 0; i < x; i++) {
            Matrix row = matrix.getRow(i);
            Matrix normVector = norm(row);
            if (i == 0) {
                matrixList = new MatrixList(normVector, true);
            } else {
                matrixList.add(normVector);
            }
        }
        return matrixList.getMatrix();
    }

    private static Matrix norm(Matrix matrix) throws Exception {
        int x = matrix.getX();
        int y = matrix.getY();
        Matrix result = new Matrix(x, y);
        float avg = matrix.getAVG();//平均值
        float sd = matrixOperation.getSdByMatrix(matrix, avg, 0.00001f);//标准差
        for (int i = 0; i < x; i++) {
            for (int j = 0; j < y; j++) {
                float value = (matrix.getNumber(i, j) - avg) / sd;
                result.setNub(i, j, value);
            }
        }
        return result;
    }

    private static Matrix back(Matrix errorMatrix, float study) throws Exception {
        int x = errorMatrix.getX();
        int y = errorMatrix.getY();
        float n = (float) Math.sqrt(x * y);
        float nt = -n / (n - 1);
        Matrix subMatrix = new Matrix(x, y);
        for (int i = 0; i < x; i++) {
            for (int j = 0; j < y; j++) {
                float subValue = errorMatrix.getNumber(i, j);
                float value = subValue * n * study + subMatrix.getNumber(i, j);
                subMatrix.setNub(i, j, value);
                for (int k = 0; k < x; k++) {
                    for (int l = 0; l < y; l++) {
                        if (k != i && l != j) {
                            float otherValue = subValue * nt * study + subMatrix.getNumber(k, l);
                            subMatrix.setNub(k, l, otherValue);
                        }
                    }
                }
            }
        }
        return subMatrix;
    }
}
